/**
 * @author banglong
 * @date 2016/12/5
 */
define(['text!../html/components/common/changePasswordTemp.html!strip', 'avalon', 'indexApi',
    'tools', 'Layer'
  ],
  function (htmlTemp, avalon, api, tools, layer) {
    return function () {
      var runtimeParam;
      var getRuntimeParam = function () {
        return {};
      };

      var vm;
      var getVmOption = function () {
        return {
          $id: 'changePassword',
          oldPassword: '',
          newPassword: '',
          rePassword: '',
          // 提交信息
          submit: function () {
            var validate = vm.validatePassword();
            if (!validate) return;

            api.ssoChangePassword({
              useName: runtimeParam.user.Ygid.slice(runtimeParam.user.Ygid.length - 5),
              oldPassword: vm.oldPassword,
              newPassword: vm.newPassword,
              editor: runtimeParam.user.Ygid.slice(runtimeParam.user.Ygid.length - 5),
              mzbz: '0'
            }).then(function(jsonResult){

              if (jsonResult.issuccess !== 0) {
                return Promise.reject(jsonResult.message);
              }

            })['catch'](function (ex) {
              tools.alterError(ex)
            })

            api.changePassword({
              className: 'AuthenticationManager',
              method: 'ModifyPassword',
              ygid: runtimeParam.user.Ygid,
              oldPassword: vm.oldPassword,
              newPassword: vm.newPassword
            }).then(function (jsonResult) {

              if (jsonResult.issuccess !== 0) {
                return Promise.reject(jsonResult.message);
              }
              tools.alterInfo('修改密码成功');
              layer.close(runtimeParam.layeroObj);
              setTimeout(function () {
                location.href = 'logout';
              }, 500)

            })['catch'](function (ex) {
              tools.alterError(ex)
            })
          },
          validatePassword: function () {
            var validate = true;
            if (!vm.oldPassword) {
              tools.alterError('请输入旧密码');
              validate = false;
            } else if (!vm.newPassword) {
              tools.alterError('请输入新密码');
              validate = false;
            } else if (!vm.rePassword) {
              tools.alterError('请输入确认密码');
              validate = false;
            } else if (vm.rePassword && vm.rePassword !== vm.newPassword) {
              tools.alterError('确认密码与新密码请保持一致');
              validate = false
            }
            return validate;
          }
        };
      };

      this.init = function (param) {
        runtimeParam = getRuntimeParam();
        runtimeParam = Object.assign({}, runtimeParam, param);

        var vmConfig = getVmOption();
        runtimeParam.layeroObj = tools.createNewOpenWindow(['460px', '360px'], '修改密码', htmlTemp,
          false, {
            vmConfig: vmConfig,
            // 销毁方法
            destroy: function () {
              runtimeParam = null;
              vm = null;
            }
          });
        vm = tools.initAvalonVm(avalon, vmConfig, '#changePassword');
      };
    };
  });